<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.jpg" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="userId">
          <el-input
            v-model="loginForm.userId"
            prefix-icon="iconfont icon-user"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- 用户名 -->
        <el-form-item prop="userName">
          <el-input
            v-model="loginForm.userName"
            prefix-icon="iconfont icon-user"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="userPassword">
          <el-input
            v-model="loginForm.userPassword"
            prefix-icon="iconfont icon-3702mima"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 权限选项 -->
        <!-- <div class="radio_qx">
          <el-radio v-model="loginForm.userRole" label=1>读者</el-radio>
        </div> -->

        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="info" @click="back">返回</el-button>
          <el-button type="primary" @click="resgister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
  
<script>
export default {
  data() {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        userName: '',
        userRole: 0,
        userId: '',
        userPassword: '',
      },
      radio: '1',
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        userId: [
          { required: true, message: '请输入注册账号', trigger: 'blur' },
          {
            min: 4,
            max: 10,
            message: '长度在 4 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
        userName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    back() {
      this.$router.push('/login')
    },
    resgister() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          return
        } else {
          const { data: res } = await this.$http.post(
            '/user/addAll',
            this.loginForm
          ) //localhost:9091/login
          console.log(res.code)
          if (res.code !== '200') return this.$message.error('注册失败！')
          else {
            this.$message.success('注册成功')
            window.sessionStorage.setItem('token', '123123')
            setTimeout(() => {
              this.$router.push('/login')
            }, 500)
          }
        }
      })
    },
  },
}
</script>
  
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.radio_qx {
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  padding: 10px 0;
  padding-bottom: 20px;
  font-size: 16px;
}
</style>
  